/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import adc_lcard.ADCE140Driver;
import dynamixel.Actuator;
import dynamixel.HostActuator;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.Timer;

/**
 *
 * @author minaev
 */
public class LearnFrame extends javax.swing.JFrame {

    LearnPositionActuator lpa;
    WardrobeAnalyser wa;
    ActScriptAnalyzer asa;
    int old_max_dpos = -1;
    int cur_max_dpos = -1;
    int cur_edit = 0;
    boolean autoupdate_sliders = false;
    final String scriptDir = "." + File.separator + "scripts" + File.separator;
    File scriptDirFile = new File(scriptDir);
    Timer max_dpos = new Timer(500, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            cur_max_dpos = lpa.getNumMaxDpos();
            if (cur_max_dpos != old_max_dpos) {

                switch (old_max_dpos) {
                    case 0: {
                        jb_a1.setBackground(Color.LIGHT_GRAY);
                        break;
                    }
                    case 1: {
                        jb_a2.setBackground(Color.LIGHT_GRAY);
                        break;
                    }
                    case 2: {
                        jb_a3.setBackground(Color.LIGHT_GRAY);
                        break;
                    }
                    case 3: {
                        jb_a4.setBackground(Color.LIGHT_GRAY);
                        break;
                    }
                }

                switch (cur_max_dpos) {
                    case 0: {
                        jb_a1.setBackground(Color.red);
                        break;
                    }
                    case 1: {
                        jb_a2.setBackground(Color.red);
                        break;
                    }
                    case 2: {
                        jb_a3.setBackground(Color.red);
                        break;
                    }
                    case 3: {
                        jb_a4.setBackground(Color.red);
                        break;
                    }
                }
                old_max_dpos = cur_max_dpos;
            }
        }
    });
    int scan_eng = 0;
    int scan_adc = 0;
    int bufeng = 0;
    Timer scan_timer = new Timer(50, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (scan_eng >= 0 && scan_adc > 0) {
                jsp_adc.addValue(ADCE140Driver.getGdata(scan_adc)/4);
                bufeng = lpa.getActuators()[scan_eng].getCurTorque();
                if(bufeng!=-1){
                    jsp_eng.addValue(bufeng);
                }
            }
        }
    });

    /**
     * Creates new form LearnFrame
     */
    public LearnFrame() {
        this(LearnFrame.getActuators());
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static Actuator[] getActuators() {
        Actuator[] acts = new Actuator[4];
        HostActuator ha = new HostActuator(57142);
        for (int i = 0; i < acts.length; i++) {
            acts[i] = new Actuator(ha, i + 1);
        }
        return acts;
    }

    public LearnFrame(Actuator[] acts) {

        lpa = new LearnPositionActuator(acts);

        wa = new WardrobeAnalyser(acts);
        asa = new ActScriptAnalyzer(acts);
        initComponents();

        lpa.torqueDisable();

        updateGUI();

        //max_dpos.start();

        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setLocationRelativeTo(null);

        jsp_adc.setGrafColor(Color.red);
        jsp_eng.setGrafColor(Color.blue);
        jsp_adc.setDrawHDig(false);
        jsp_eng.setDrawHDig(false);
        jsp_adc.addValue(1024);
        jsp_eng.addValue(1024);
        jsp_adc.addValue(0);
        jsp_eng.addValue(0);

    }

    private void updateGUI() {
        jtf_speed.setText(lpa.getSpeed() + "");
        jtf_tor.setText(lpa.getTor() + "");
        jtb_scan.setSelected(lpa.isScan());
        jl_curspeed.setText(lpa.getSpeed() + "");
        jl_curtor.setText(lpa.getTor() + "");
        jta_script.setText(lpa.getScript());
        jl_script.updateUI();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jb_a1 = new javax.swing.JButton();
        jb_a2 = new javax.swing.JButton();
        jb_a3 = new javax.swing.JButton();
        jb_a4 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jl_curspeed = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jl_curtor = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jb_speed = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jtf_speed = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jb_tor = new javax.swing.JButton();
        jtf_tor = new javax.swing.JTextField();
        jtb_scan = new javax.swing.JToggleButton();
        jb_torqDisable = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jta_script = new javax.swing.JTextArea();
        jPanel5 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jl_script = new javax.swing.JList();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jb_step = new javax.swing.JButton();
        jb_load = new javax.swing.JButton();
        jb_save = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jl_act_num = new javax.swing.JLabel();
        jPanel8 = new javax.swing.JPanel();
        jtf_act_pos = new javax.swing.JTextField();
        jb_act_pos = new javax.swing.JButton();
        js_act_pos = new javax.swing.JSlider();
        jPanel9 = new javax.swing.JPanel();
        jtf_act_speed = new javax.swing.JTextField();
        jb_act_speed = new javax.swing.JButton();
        js_act_speed = new javax.swing.JSlider();
        jPanel10 = new javax.swing.JPanel();
        jtf_act_tor = new javax.swing.JTextField();
        jb_act_tor = new javax.swing.JButton();
        js_act_tor = new javax.swing.JSlider();
        jPanel11 = new javax.swing.JPanel();
        jtf_act_chn = new javax.swing.JTextField();
        jb_act_chn = new javax.swing.JButton();
        js_act_chn = new javax.swing.JSlider();
        jPanel12 = new javax.swing.JPanel();
        jPanel13 = new javax.swing.JPanel();
        jch_scan = new javax.swing.JCheckBox();
        jsp_adc = new org.min.gui.JSlidePlotter();
        jsp_eng = new org.min.gui.JSlidePlotter();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jb_a1.setText("1");
        jb_a1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_a1ActionPerformed(evt);
            }
        });

        jb_a2.setText("2");
        jb_a2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_a2ActionPerformed(evt);
            }
        });

        jb_a3.setText("3");
        jb_a3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_a3ActionPerformed(evt);
            }
        });

        jb_a4.setText("4");
        jb_a4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_a4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jb_a1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_a2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_a3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_a4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jb_a1)
                    .addComponent(jb_a2)
                    .addComponent(jb_a3)
                    .addComponent(jb_a4))
                .addContainerGap())
        );

        jLabel1.setText("Cur Speed:");

        jl_curspeed.setText("0");

        jLabel3.setText("Cur Tor:");

        jl_curtor.setText("0");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jl_curspeed))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jl_curtor)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jl_curspeed))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jl_curtor))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jb_speed.setText("Apply");
        jb_speed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_speedActionPerformed(evt);
            }
        });

        jLabel2.setText("Speed:");

        jtf_speed.setText("0");

        jLabel4.setText("Tor:");

        jb_tor.setText("Apply");
        jb_tor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_torActionPerformed(evt);
            }
        });

        jtf_tor.setText("0");

        jtb_scan.setText("Scan");
        jtb_scan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtb_scanActionPerformed(evt);
            }
        });
        jtb_scan.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jtb_scanKeyReleased(evt);
            }
        });

        jb_torqDisable.setText("torqDisable");
        jb_torqDisable.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_torqDisableActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtf_speed)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_speed))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtf_tor)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_tor))
            .addComponent(jtb_scan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jb_torqDisable, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jb_speed)
                    .addComponent(jLabel2)
                    .addComponent(jtf_speed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jb_tor)
                    .addComponent(jtf_tor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtb_scan)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jb_torqDisable)
                .addContainerGap())
        );

        jta_script.setColumns(20);
        jta_script.setRows(5);
        jScrollPane1.setViewportView(jta_script);

        jButton1.setText("-");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jl_script.setModel(lpa);
        jl_script.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jl_scriptValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jl_script);

        jButton2.setText("--");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Go");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jb_step.setText("Step");
        jb_step.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_stepActionPerformed(evt);
            }
        });

        jb_load.setText("Load");
        jb_load.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_loadActionPerformed(evt);
            }
        });

        jb_save.setText("Save");
        jb_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_saveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jb_step)
                    .addComponent(jb_load)
                    .addComponent(jb_save)))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_step)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_load)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_save)
                .addContainerGap(30, Short.MAX_VALUE))
            .addComponent(jScrollPane2)
        );

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel5.setText("Motion:");

        jl_act_num.setText("0");

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder("Position"));

        jtf_act_pos.setText("0");

        jb_act_pos.setText("Apply");
        jb_act_pos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_act_posActionPerformed(evt);
            }
        });

        js_act_pos.setMaximum(4095);
        js_act_pos.setValue(0);
        js_act_pos.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                js_act_posStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addComponent(jtf_act_pos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_act_pos))
            .addComponent(js_act_pos, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtf_act_pos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jb_act_pos))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(js_act_pos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel9.setBorder(javax.swing.BorderFactory.createTitledBorder("Speed"));

        jtf_act_speed.setText("0");

        jb_act_speed.setText("Apply");
        jb_act_speed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_act_speedActionPerformed(evt);
            }
        });

        js_act_speed.setMaximum(1023);
        js_act_speed.setValue(0);
        js_act_speed.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                js_act_speedStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addComponent(jtf_act_speed)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_act_speed))
            .addComponent(js_act_speed, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtf_act_speed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jb_act_speed))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(js_act_speed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel10.setBorder(javax.swing.BorderFactory.createTitledBorder("Torque"));

        jtf_act_tor.setText("0");

        jb_act_tor.setText("Apply");
        jb_act_tor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_act_torActionPerformed(evt);
            }
        });

        js_act_tor.setMaximum(1023);
        js_act_tor.setValue(0);
        js_act_tor.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                js_act_torStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
        jPanel10.setLayout(jPanel10Layout);
        jPanel10Layout.setHorizontalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addComponent(jtf_act_tor)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_act_tor))
            .addComponent(js_act_tor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel10Layout.setVerticalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtf_act_tor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jb_act_tor))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(js_act_tor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel11.setBorder(javax.swing.BorderFactory.createTitledBorder("Scan channel"));

        jtf_act_chn.setText("0");

        jb_act_chn.setText("Apply");
        jb_act_chn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_act_chnActionPerformed(evt);
            }
        });

        js_act_chn.setMaximum(16);
        js_act_chn.setValue(0);
        js_act_chn.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                js_act_chnStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel11Layout = new javax.swing.GroupLayout(jPanel11);
        jPanel11.setLayout(jPanel11Layout);
        jPanel11Layout.setHorizontalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel11Layout.createSequentialGroup()
                .addComponent(jtf_act_chn)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jb_act_chn))
            .addComponent(js_act_chn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel11Layout.setVerticalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel11Layout.createSequentialGroup()
                .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtf_act_chn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jb_act_chn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(js_act_chn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel10, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jl_act_num)
                        .addGap(0, 207, Short.MAX_VALUE))
                    .addComponent(jPanel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jl_act_num))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jch_scan.setText("Scan");
        jch_scan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jch_scanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel13Layout = new javax.swing.GroupLayout(jPanel13);
        jPanel13.setLayout(jPanel13Layout);
        jPanel13Layout.setHorizontalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel13Layout.createSequentialGroup()
                .addComponent(jch_scan)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        jPanel13Layout.setVerticalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jch_scan)
        );

        javax.swing.GroupLayout jsp_adcLayout = new javax.swing.GroupLayout(jsp_adc);
        jsp_adc.setLayout(jsp_adcLayout);
        jsp_adcLayout.setHorizontalGroup(
            jsp_adcLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jsp_adcLayout.setVerticalGroup(
            jsp_adcLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jsp_engLayout = new javax.swing.GroupLayout(jsp_eng);
        jsp_eng.setLayout(jsp_engLayout);
        jsp_engLayout.setHorizontalGroup(
            jsp_engLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jsp_engLayout.setVerticalGroup(
            jsp_engLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel12Layout = new javax.swing.GroupLayout(jPanel12);
        jPanel12.setLayout(jPanel12Layout);
        jPanel12Layout.setHorizontalGroup(
            jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel12Layout.createSequentialGroup()
                .addComponent(jsp_adc, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jsp_eng, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(2, 2, 2))
        );
        jPanel12Layout.setVerticalGroup(
            jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel12Layout.createSequentialGroup()
                .addComponent(jPanel13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jsp_adc, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jsp_eng, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(0, 0, 0))
        );

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jb_speedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_speedActionPerformed
        lpa.setSpeed(Integer.valueOf(jtf_speed.getText()));
        updateGUI();
    }//GEN-LAST:event_jb_speedActionPerformed

    private void jb_torActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_torActionPerformed
        lpa.setTor(Integer.valueOf(jtf_tor.getText()));
        updateGUI();
    }//GEN-LAST:event_jb_torActionPerformed

    private void jtb_scanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtb_scanActionPerformed
        lpa.setScaning(Integer.valueOf(jtf_act_chn.getText()));
        updateGUI();
    }//GEN-LAST:event_jtb_scanActionPerformed

    private void jb_a1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_a1ActionPerformed
        lpa.learn(0, jl_script.getSelectedIndex());
        incSelectedLine();
        updateGUI();
    }//GEN-LAST:event_jb_a1ActionPerformed

    private void jb_a2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_a2ActionPerformed
        lpa.learn(1, jl_script.getSelectedIndex());
        incSelectedLine();
        updateGUI();
    }//GEN-LAST:event_jb_a2ActionPerformed

    private void jb_a3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_a3ActionPerformed
        lpa.learn(2, jl_script.getSelectedIndex());
        incSelectedLine();
        updateGUI();
    }//GEN-LAST:event_jb_a3ActionPerformed

    private void jb_a4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_a4ActionPerformed
        lpa.learn(3, jl_script.getSelectedIndex());
        incSelectedLine();
        updateGUI();
    }//GEN-LAST:event_jb_a4ActionPerformed

    private void jtb_scanKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtb_scanKeyReleased
        switch (evt.getKeyChar()) {
            case '1': {
                this.jb_a1ActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null));
                break;
            }
            case '2': {
                this.jb_a2ActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null));
                break;
            }
            case '3': {
                this.jb_a3ActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null));
                break;
            }
            case '4': {
                this.jb_a4ActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, null));
                break;
            }
        }

    }//GEN-LAST:event_jtb_scanKeyReleased

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int ind = jl_script.getSelectedIndex();
        if (ind != -1) {
            lpa.remove(ind);
            updateGUI();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        lpa.removeAll();
        updateGUI();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        //wa.analys(lpa.getScript());
        asa.analyzeScript(lpa.getScript());
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jb_torqDisableActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_torqDisableActionPerformed
        lpa.torqueDisable();
    }//GEN-LAST:event_jb_torqDisableActionPerformed

    private void jl_scriptValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jl_scriptValueChanged
        if (!evt.getValueIsAdjusting()) {
            int ind = jl_script.getSelectedIndex();
            if (ind != -1) {
                cur_edit = ind;
                updateEditForm(ind);
            }
        }
    }//GEN-LAST:event_jl_scriptValueChanged

    private void jb_stepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_stepActionPerformed
        runStep(1);
    }//GEN-LAST:event_jb_stepActionPerformed

    private void js_act_posStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_js_act_posStateChanged
        jtf_act_pos.setText("" + js_act_pos.getValue());
        if (!autoupdate_sliders) {
            jb_act_posActionPerformed(null);
        }
    }//GEN-LAST:event_js_act_posStateChanged

    private void js_act_speedStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_js_act_speedStateChanged
        jtf_act_speed.setText("" + js_act_speed.getValue());
        //jb_act_speedActionPerformed(null);
    }//GEN-LAST:event_js_act_speedStateChanged

    private void js_act_torStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_js_act_torStateChanged
        jtf_act_tor.setText("" + js_act_tor.getValue());
        //jb_act_torActionPerformed(null);
    }//GEN-LAST:event_js_act_torStateChanged

    private void jb_act_posActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_act_posActionPerformed
        lpa.getActuatorPos().set(cur_edit, Integer.valueOf(jtf_act_pos.getText()));
        updateGUI();
        runStep(0);
    }//GEN-LAST:event_jb_act_posActionPerformed

    private void jb_act_speedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_act_speedActionPerformed
        lpa.getActuatorSpeed().set(cur_edit, Integer.valueOf(jtf_act_speed.getText()));
        updateGUI();
        //runStep(0);
    }//GEN-LAST:event_jb_act_speedActionPerformed

    private void jb_act_torActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_act_torActionPerformed
        lpa.getActuatorTor().set(cur_edit, Integer.valueOf(jtf_act_tor.getText()));
        updateGUI();
        //runStep(0);
    }//GEN-LAST:event_jb_act_torActionPerformed

    private void jb_loadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_loadActionPerformed
        JFileChooser fch = new JFileChooser();
        if (scriptDirFile.exists()) {
            fch.setCurrentDirectory(scriptDirFile);
        }
        if (fch.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
            try {
                FileInputStream fis = new FileInputStream(fch.getSelectedFile());
                int buf = 0;
                String sbuf = "";
                while ((buf = fis.read()) != -1) {
                    sbuf += (char) buf;
                }
                fis.close();

                lpa.loadScript(sbuf);
                updateGUI();

            } catch (Exception ex) {
                System.err.println("Error in read file");
            }
        }
    }//GEN-LAST:event_jb_loadActionPerformed

    private void jb_saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_saveActionPerformed
        JFileChooser fch = new JFileChooser();
        if (scriptDirFile.exists()) {
            fch.setCurrentDirectory(scriptDirFile);
        }
        if (fch.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            try {
                FileOutputStream fos = new FileOutputStream(fch.getSelectedFile());
                String sbuf = lpa.getScript();
                for (int i = 0; i < sbuf.length(); i++) {
                    fos.write(sbuf.charAt(i));
                }
                fos.close();
            } catch (Exception ex) {
                System.err.println("Error in save script");
            }
        }
    }//GEN-LAST:event_jb_saveActionPerformed

    private void jb_act_chnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_act_chnActionPerformed
        lpa.getActuatorScan().set(cur_edit, Integer.valueOf(jtf_act_chn.getText()));
        updateGUI();
    }//GEN-LAST:event_jb_act_chnActionPerformed

    private void js_act_chnStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_js_act_chnStateChanged
        jtf_act_chn.setText("" + js_act_chn.getValue());
    }//GEN-LAST:event_js_act_chnStateChanged

    private void jch_scanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jch_scanActionPerformed
        if (jch_scan.isSelected()) {
            if (scan_adc > 0 && scan_eng >= 0 && !scan_timer.isRunning()) {
                scan_timer.start();
            } else {
                jch_scan.setSelected(false);
            }
        } else {
            if (scan_timer.isRunning()) {
                scan_timer.stop();
            }
        }
    }//GEN-LAST:event_jch_scanActionPerformed

    private void updateEditForm(int ind) {
        autoupdate_sliders = true;
        jl_act_num.setText("" + ind);
        jtf_act_pos.setText(lpa.getActuatorPos().get(ind).toString());
        js_act_pos.setValue(lpa.getActuatorPos().get(ind).intValue());
        jtf_act_speed.setText(lpa.getActuatorSpeed().get(ind).toString());
        js_act_speed.setValue(lpa.getActuatorSpeed().get(ind).intValue());
        jtf_act_tor.setText(lpa.getActuatorTor().get(ind).toString());
        js_act_tor.setValue(lpa.getActuatorTor().get(ind).intValue());
        jtf_act_chn.setText(lpa.getActuatorScan().get(ind).toString());
        js_act_chn.setValue(lpa.getActuatorScan().get(ind).intValue());
        scan_adc = lpa.getActuatorScan().get(ind).intValue();
        scan_eng = lpa.getActuatorNums().get(ind).intValue();
        autoupdate_sliders = false;
    }

    public LearnPositionActuator getLearnPositionActuator() {
        return lpa;
    }

    private void runStep(int d) {
        int ind = jl_script.getSelectedIndex();
        if (ind != -1) {
            //System.out.println("ind: " + ind + " element: " + lpa.getElementAt(ind));
            wa.analysLine(lpa.getElementAt(ind));
            if (ind + 1 < lpa.getSize()) {
                jl_script.setSelectedIndex(ind + d);
            }
        }
    }

    private void incSelectedLine() {
        int ind = jl_script.getSelectedIndex();
        if (ind != -1) {
            if (ind + 1 < lpa.getSize()) {
                jl_script.setSelectedIndex(ind + 1);
            }
        }
    }

    public String getScript() {
        updateGUI();
        return jta_script.getText();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LearnFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LearnFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LearnFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LearnFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LearnFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jb_a1;
    private javax.swing.JButton jb_a2;
    private javax.swing.JButton jb_a3;
    private javax.swing.JButton jb_a4;
    private javax.swing.JButton jb_act_chn;
    private javax.swing.JButton jb_act_pos;
    private javax.swing.JButton jb_act_speed;
    private javax.swing.JButton jb_act_tor;
    private javax.swing.JButton jb_load;
    private javax.swing.JButton jb_save;
    private javax.swing.JButton jb_speed;
    private javax.swing.JButton jb_step;
    private javax.swing.JButton jb_tor;
    private javax.swing.JButton jb_torqDisable;
    private javax.swing.JCheckBox jch_scan;
    private javax.swing.JLabel jl_act_num;
    private javax.swing.JLabel jl_curspeed;
    private javax.swing.JLabel jl_curtor;
    private javax.swing.JList jl_script;
    private javax.swing.JSlider js_act_chn;
    private javax.swing.JSlider js_act_pos;
    private javax.swing.JSlider js_act_speed;
    private javax.swing.JSlider js_act_tor;
    private org.min.gui.JSlidePlotter jsp_adc;
    private org.min.gui.JSlidePlotter jsp_eng;
    private javax.swing.JTextArea jta_script;
    private javax.swing.JToggleButton jtb_scan;
    private javax.swing.JTextField jtf_act_chn;
    private javax.swing.JTextField jtf_act_pos;
    private javax.swing.JTextField jtf_act_speed;
    private javax.swing.JTextField jtf_act_tor;
    private javax.swing.JTextField jtf_speed;
    private javax.swing.JTextField jtf_tor;
    // End of variables declaration//GEN-END:variables
}
